﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YesBlog.Framework.Extension;

namespace YesBlog.Framework.Pager
{
    public static class Extensions
    {
        /// <summary>
        /// 分页 
        /// </summary>
        /// <typeparam name="T">要返回的实体类型</typeparam>
        /// <param name="query"></param>
        /// <param name="page">第几页</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="count"></param>
        /// <returns></returns>
        public static PageInfo<T> Paged<T>(this IQueryable<T> query, int page, int pageSize, int count)
        {
            if (page <= 0)
            {
                page = 1;
            }
            if (pageSize <= 0)
            {
                pageSize = 10;
            }
            return new PageInfo<T>()
            {
                PageIndex = page,
                Count = count,
                PageCount = count % pageSize == 0 ? count / pageSize : (count / pageSize + 1),
                List = query.Skip((page - 1) * pageSize).Take(pageSize).ToList()
            };
        }
        /// <summary>
        /// 分页
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="query"></param>
        /// <param name="page"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public static PageInfo<TResult> Paged<T,TResult>(this IQueryable<T> query, int page, int pageSize)
        {
            var count = query.Count();
            return new PageInfo<TResult>()
            {
                PageIndex = page,
                Count = count,
                PageCount = count % pageSize == 0 ? count / pageSize : (count / pageSize + 1) ,
                List = query.Skip((page - 1) * pageSize).Take(pageSize).MapToList<TResult>().ToList()
            };
        }
    }
}
